package org.jrpc.core.registry;

import org.jrpc.base.ServiceInfo;
import org.jrpc.base.ServiceInfoManage;
import org.jrpc.core.event.DownServiceEvent;
import org.jrpc.core.event.UpServiceEvent;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 实现服务信息的管理
 */
public class ZooKeeperServiceInfoManage implements ServiceInfoManage,ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Map<String,ServiceInfo> serviceInfoMap = new ConcurrentHashMap<>();

/*    private ZooKeeperServiceInfoManage zooKeeperServiceInfoManage;

    public ZooKeeperServiceInfoManage(ZooKeeperServiceInfoManage zooKeeperServiceInfoManage){
        this.zooKeeperServiceInfoManage=zooKeeperServiceInfoManage;
    }*/

    @Override
    public List<ServiceInfo> getServiceInfoAllList() {
        List<ServiceInfo> list = new ArrayList<>();
        serviceInfoMap.forEach((k,v)->{
            list.add(v);
        });
        return list;
    }

    @Override
    public List<ServiceInfo> getServiceInfoByServiceName(String serviceName) {
        List<ServiceInfo> list = new ArrayList<>();
        serviceInfoMap.forEach((k,v)->{
            if(v.getServiceName().equals(serviceName)){
                list.add(v);
            }
        });
        return list;
    }

    @Override
    public List<ServiceInfo> getServiceInfoByVersion(String version) {
        List<ServiceInfo> list = new ArrayList<>();
        serviceInfoMap.forEach((k,v)->{
            if(v.getVaersion().equals(version)){
                list.add(v);
            }
        });
        return list;
    }

    @Override
    public List<ServiceInfo> getServiceInfoByGroup(String group) {
        List<ServiceInfo> list = new ArrayList<>();
        serviceInfoMap.forEach((k,v)->{
            if(v.getGroup().equals(group)){
                list.add(v);
            }
        });
        return list;
    }

    @Override
    public ServiceInfo getServiceInfo(String id) {
        return serviceInfoMap.get(id);
    }

    @Override
    public void setServiceInfoList(List<ServiceInfo> list) {
        list.forEach(x->{
            serviceInfoMap.put(x.getId(),x);
        });
    }

    //上线服务，上线事件  默认推送全部，有group推group，再到里面的version
    @Override
    public void setUPServiceInfo(ServiceInfo serviceInfo) {
        ServiceInfo sInfo = serviceInfoMap.get(serviceInfo.getId());
        sInfo.setStatus(serviceInfo.getStatus());
        this.applicationContext.publishEvent(new UpServiceEvent(this,sInfo));
    }

    //下线服务，下线事件  默认推送全部，有group推group，再到里面的version
    @Override
    public void setDOWNServiceInfoD(ServiceInfo serviceInfo) {
        ServiceInfo sInfo = serviceInfoMap.get(serviceInfo.getId());
        sInfo.setStatus(serviceInfo.getStatus());
        this.applicationContext.publishEvent(new DownServiceEvent(this,sInfo));
    }

    //修改版本号，修改版本事件  默认推送全部，有group推group，再到里面的version
    @Override
    public void setVersionServiceInfo(ServiceInfo serviceInfo) {

    }

    //设置分组，分组事件  默认推送全部，有group推group，再到里面的version
    @Override
    public void setGroupServiceInfo(ServiceInfo serviceInfo) {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
